package cn.xlbweb.cli.controller;

import cn.xlbweb.cli.common.ServerResponse;
import cn.xlbweb.cli.common.TableResponse;
import cn.xlbweb.cli.common.TreeResponse;
import cn.xlbweb.cli.pojo.dto.DeleteRoleDTO;
import cn.xlbweb.cli.pojo.dto.InsertRoleDTO;
import cn.xlbweb.cli.pojo.dto.ListRoleDTO;
import cn.xlbweb.cli.pojo.dto.UpdateRoleDTO;
import cn.xlbweb.cli.service.IRoleService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author: bobi
 * @date: 2019-02-04 00:55
 * @description:
 */
@Api(tags = "角色模块")
@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private IRoleService iRoleService;

    @GetMapping("listRole")
    @RequiresPermissions("role:listRole")
    public TableResponse listRole(@Valid ListRoleDTO listRoleDTO) {
        return iRoleService.listRole(listRoleDTO);
    }

    @PostMapping("insertRole")
    @RequiresPermissions("role:insertRole")
    public ServerResponse insertRole(@Valid InsertRoleDTO insertRoleDTO) {
        return iRoleService.insertRole(insertRoleDTO);
    }

    @PutMapping("updateRole")
    @RequiresPermissions("role:updateRole")
    public ServerResponse updateRole(@Valid UpdateRoleDTO updateRoleDTO) {
        return iRoleService.updateRole(updateRoleDTO);
    }

    @DeleteMapping("deleteRole")
    @RequiresPermissions("role:deleteRole")
    public ServerResponse deleteRole(@Valid DeleteRoleDTO deleteRoleDTO) {
        return iRoleService.deleteRole(deleteRoleDTO);
    }

    @GetMapping("listRoleName")
    public ServerResponse listRoleName() {
        return iRoleService.listRoleName();
    }


    @GetMapping("listAuthorize")
    @RequiresPermissions("role:listAuthorize")
    public TreeResponse listAuthorize(Integer roleId) {
        return iRoleService.listAuthorize(roleId);
    }

    @PostMapping("authorize")
    @RequiresPermissions("role:authorize")
    public ServerResponse authorize(Integer roleId, String permissionIds) {
        return iRoleService.authorize(roleId, permissionIds);
    }

    @GetMapping("listAssign")
    @RequiresPermissions("role:listAssign")
    public ServerResponse listAssign(Integer roleId) {
        return iRoleService.listAssign(roleId);
    }

    @PostMapping("assign")
    @RequiresPermissions("role:assign")
    public ServerResponse assign(Integer roleId, String userIds) {
        return iRoleService.assign(roleId, userIds);
    }
}
